package yxy.game.pm2.active.command.RecruitPackage;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.ActiveModule_招募令礼包;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.active.module.subordinateclass.RecruitPackage;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

@MessageType(proto = Command.ActRecruitPackage, classtype = ActiveMessage.ActRecruitPackageRequest.class)
public class CMD_Active_招募令礼包_Info extends Command<ActiveMessage.ActRecruitPackageRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActRecruitPackageRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActRecruitPackageResponse.Builder response = ActiveMessage.ActRecruitPackageResponse.newBuilder();

        ActiveModule_招募令礼包 am = ActiveModule_招募令礼包.get(playercode);
        if(am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if(!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        int packageNum = 0; //售完礼包数

        for(RecruitPackage dgift : am.getDgifts()) {
            ActiveMessage.RecruitPackage.Builder build = ActiveMessage.RecruitPackage.newBuilder();

            if (dgift.getBuyNum() == dgift.getNumlimit()){
                packageNum++;
            }

            build.setGoodsid(dgift.getGoodsid());
            build.setPackageName(dgift.getPackageName());
            build.setStarttime(dgift.getStart().getMillis());
            build.setEndtime(dgift.getEnd().getMillis());

            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(dgift.getCurs());
            build.addAllCurs(items);

            int canget = dgift.isHadbuy() ? DrawStatus.hadget.getStatus() : dgift.isLock() ? DrawStatus.cannotget.getStatus() : DrawStatus.canget.getStatus();
            build.setCanget(canget);
            build.setBuyNum(dgift.getBuyNum()); //已购买次数
            build.setNumLimit(dgift.getNumlimit()); //限购次数
            if (dgift.isCanBuy()){
                build.setCanBuy(1); //可购买
            }else {
                build.setCanBuy(0); //不可购买
            }

            //礼包售完，活动关闭
            if (packageNum == am.getDgifts().size()){
                new ActOpenHandle().updateActOpen(playercode, ActiveModule_招募令礼包.acttype, OpenType.关闭);
            }


            response.addDgifts(build.build());
        }



        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActRecruitPackageResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
